<?php

namespace app\services\face;

use app\model\company\Company;
use app\model\plan\Plan;
use app\model\plan\PlanStaff;
use app\model\plan\snap\Snap;
use app\model\staff\Staff;
use lslcoded\qrcode\QRcode;
use think\Exception;
use think\facade\App;
use think\facade\Config;
use think\facade\Db;
use think\Image;

class CertService
{
    public $ret  ;
    protected $errorMsg = '' ;
    protected $imgUrl = '' ;

    public  function replace_domain($url)
    {
        $url =  str_replace(request()->domain() . '/' , '' , $url) ;
        return $url ;
    }

    public function jieyeCheck($planId = 0 , $staffId = 0, $sel = 0)
    {
        $planStaff = PlanStaff::planStaffInfo( $planId , $staffId);

        if($planStaff == null)
        {
            $this->errorMsg = '暂无结业数据' ;
            return false ;
        }
        if($planStaff['complete_img_url'] != '')
        {
            $this->imgUrl = $planStaff['complete_img_url'] ;
            return true ;
        }

        if($planStaff['progress'] != 100)
        {
            $this->errorMsg = '当前课程未学习完成' ;
            return false ;
        }
        $list = Snap::snapImage($planId , $staffId ,ParaConstant::SNAP_POST) ;
        $total2 = count($list) ;
        if($total2 < 3)
        {
            $this->errorMsg = '抓拍张数不够3张，不可生成结业证' ;
            return false ;
        }

        if($planStaff['code'] == '')
        {
            try{

                Db::startTrans() ;
//
//                $stat = Db::name('stat')->where('year' , date('Y'))->find() ;
//                $total =$stat['total'] + 1 ;
//
//                $ret = Db::name('stat')->where('year' , date('Y'))->where('ver' , $stat['ver'])
//                    ->inc('total' , 1)
//                    ->update() ;
//                if(!$ret ){
//                    Db::rollback() ;
//                    $this->errorMsg = '请重新生成' ;
//                    return false ;
//                }
                $total = 1 ;
                $code =  str_pad($total, 6, '0', STR_PAD_LEFT);

                $cn = Config::get('para.cert_cn') ;

                PlanStaff::where('id' , $planStaff['id'])->update([
                    'complete_img_status' => 9 ,
                    'code' => $cn . date('Y'). $code ,
                    'complete_img_time' => time() ,
                ]) ;
                Db::commit() ;
            }

            catch (Exception $e)
            {
                Db::rollback() ;
                $this->errorMsg = $e->getMessage() ;
                return false ;
            }
        }

        return  $this->jieye($planId , $staffId , $sel) ;

    }
    public function jieye($planId = 0 , $staffId = 0 , $sel = 0)
    {

        $plan = Plan::where('id' , $planId)->find() ;
        $staffInfo = Staff::where('id' ,$staffId)->find() ;

        $planStaff = PlanStaff::planStaffInfo( $planId , $staffId);


        $date = strtotime($planStaff['study_end_time'] );


        $strtotime = strtotime($planStaff['expire_time']) ;

        $parentImg = 'uploads/cert/bg.jpg' ;

        $baseImg = Image::open($parentImg) ;
        $ttf = 'uploads/cert/simsun.ttc' ;

        $imgUrl = $this->replace_domain($staffInfo['img_url']) ;


        $merchant = Company::getOneName($planStaff['company_id'] , 'company_name') ;
        $baseImg->text($staffInfo['username'] , $ttf, 30 , '#000000' , [450,655]) ;
        $baseImg->text($staffInfo['sex'] , $ttf, 30 , '#000000' , [1070,655]) ;
        $baseImg->text($staffInfo['idcard'] , $ttf, 30 , '#000000' , [450,744]) ;
        $baseImg->text($merchant , $ttf, 24 , '#000000' , [450,824]) ;


        $baseImg->text($planStaff['code'] , $ttf, 30 , '#000000' , [720 ,520]) ;
//        $baseImg->text(date('Y-m-d' , strtotime($planStaff['study_end_time'])) , $ttf, 30 , '#000000' , [1090 ,995]) ;
        $baseImg->text(date('Y年m月d日' , strtotime($planStaff['create_time'])) , $ttf, 30 , '#000000' , [470 ,990]) ;
        $baseImg->text(date('Y' , $strtotime) , $ttf, 28 , '#000000' , [450,1160]) ;
        $baseImg->text(date('m' , $strtotime) , $ttf, 28 , '#000000' , [575,1160]) ;
        $baseImg->text(date('d' , $strtotime) , $ttf, 28 , '#000000' , [660,1160]) ;



        $baseImg->water($imgUrl , [1240 , 648]) ;
        $list = [] ;
        if($sel == 1)
        {
            $list = Snap::snapSelImage($planId , $staffId , Snap::TYPE_POST) ;
        }
        else{
            $list = Snap::snapImage($planId , $staffId , Snap::TYPE_POST) ;
        }
        $total = count($list) ;

        if($total < 3)
        {
            $this->errorMsg = '抓拍张数不够3张，不可生成结业证' ;
            return false ;
        }

        $snap1 = $list[0] ;
        $snap2 = $list[floor(($total - 1) / 2)] ;
        $snap3 = $list[$total - 1] ;

        if($snap1)
        {
            $img1 =  $this->replace_domain($snap1['img_url']) ;
            $baseImg->water($img1 , [402 , 1335]) ;
        }
        if($snap2){
            $img2 =  $this->replace_domain($snap2['img_url']) ;
            $baseImg->water($img2 , [778 , 1335]) ;

        }
        if($snap3) {
            $img3 = $this->replace_domain($snap3['img_url']);
            $baseImg->water($img3, [1162, 1335]);
        }

        $baseImg->text(date('Y' , $date) , $ttf, 28 , '#000000' , [940,2085]) ;
        $baseImg->text(date('m' , $date) , $ttf, 28 , '#000000' , [1060,2085]) ;
        $baseImg->text(date('d' , $date) , $ttf, 28 , '#000000' , [1140,2085]) ;


        $baseImg->text(date('Y-m-d H:i' , strtotime($snap1['create_time']))  , $ttf, 26 , '#000000' , [366,1660]) ;
        $baseImg->text(date('Y-m-d H:i' , strtotime($snap2['create_time'])) , $ttf, 26 , '#000000' , [748,1660]) ;
        $baseImg->text(date('Y-m-d H:i' , strtotime($snap3['create_time'])) , $ttf, 26 , '#000000' , [1132,1660]) ;

        $folderPath = 'uploads/cert/' . $planId . '/' ;
        if (!is_dir($folderPath)) {
            mkdir($folderPath, 0755, true) ;
        }

        $path = $folderPath . time() . rand(10000,99999) .'.'. $baseImg->type() ;
        $baseImg->save(App::getRootPath().'public/' .$path) ;


        $dayinImgUrl = request()->domain() . '/' . $path  ;




        $planStaff->complete_img_url = $dayinImgUrl ;
        $planStaff->save() ;


        $this->imgUrl =  $dayinImgUrl ;

        return true ;

    }


    public function getError(){
        return $this->errorMsg  ;
    }
    public function getImageUrl(){
        return $this->imgUrl  ;
    }
    public function getRet(){
        return $this->ret  ;
    }
}